package org.game.module.event.listener;

import java.util.Calendar;
import java.util.List;

import org.constant.EventConditionKey;
import org.constant.EventConditionType;
import org.constant.EventType;
import org.game.db.bean.Building;
import org.game.db.bean.Player;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventListener;
import org.game.plugin.event.GameEventStack;
import org.game.plugin.event.GameEventType;

import com.sojoys.artifact.tools.ToolMap;
import org.utils.TimeUtil;

/**
 * @author : DengYing
 * @CreateDate : 2018年1月11日 下午2:59:44
 * @Description ：建筑事件
 */
@GameEventType(EventType.BUILDING_EVENT)
public class BuildingEventListenner implements GameEventListener {

	@Override
	public void onEvent(GameEvent event) {
		GameEventStack eventStack = event.peek();
		switch (eventStack.getConditionType()) {
			case EventConditionType.WORK_STAGE:{
				if (checkTriggerType(event.getIn(), eventStack.getConditionType())){
					boolean bool = verify_eq(EventConditionKey.BUILD_CID, event.getIn(), eventStack.getParams());
					bool &= verify_eq(EventConditionKey.BUILD_LVL, event.getIn(), eventStack.getParams());
					if (bool){
						eventStack.changeProgress(1);
					}
				}
				break;
			}
		case EventConditionType.BUILDING_LVL_COUNT: {
			if (checkTriggerType(event.getIn(), eventStack.getConditionType())){
				boolean bool = verify_eq(EventConditionKey.BUILD_CID, event.getIn(), eventStack.getParams());
				int oldLv = ToolMap.getInt(EventConditionKey.OLD_LEVEL, event.getIn());
				int newLv = ToolMap.getInt(EventConditionKey.NOW_LEVEL, event.getIn());
				int limitLvl = ToolMap.getInt(EventConditionKey.LIMIT_LEVEL, eventStack.getParams());
				if (oldLv < limitLvl && newLv >= limitLvl) {
					bool &= true;
				}else {
					bool &= false;
				}

				if (bool) {
					eventStack.changeProgress(1);
				}
			}
			break;
		}
		case EventConditionType.RECEIVE_WORK_REWARD_COUNT: {
			if (checkTriggerType(event.getIn(), eventStack.getConditionType())){
				boolean bool = verify_eq(EventConditionKey.BUILD_CID, event.getIn(), eventStack.getParams());
				if (bool) {
					eventStack.changeProgress(1);
				}
			}
			break;
		}
		case EventConditionType.WORK_TIME: {
			if (checkTriggerType(event.getIn(), eventStack.getConditionType())){
				boolean bool = verify_eq(EventConditionKey.BUILD_CID, event.getIn(), eventStack.getParams());
				int timeType = (int) eventStack.getParams().get(EventConditionKey.TIME_UNIT);
				if (bool) {
					int workTime = ToolMap.getInt(EventConditionKey.WORK_TIME, event.getIn());
					int betweenTime = TimeUtil.getBetweenTime(workTime, Calendar.SECOND, TimeUtil.getDateUnit(timeType));
					eventStack.changeProgress(betweenTime);
				}
			}
			break;
		}
		}
	}
	
	@Override
	public void initEventCondition(Player player, GameEventStack eventStack) {
		switch (eventStack.getConditionType()) {
		case EventConditionType.BUILDING_LVL_COUNT: {
			int count = 0;
			int buildingCid = ToolMap.getInt(EventConditionKey.BUILD_CID, eventStack.getParams());
			int limitLvl = ToolMap.getInt(EventConditionKey.LIMIT_LEVEL, eventStack.getParams());
			List<Building> buildings = player.getBuildingsFkPlayerId();
			for (Building building : buildings) {
				if (buildingCid!=0 && buildingCid!=building.getBuildingProxy().getBuildId()) {
					continue;
				}
				if (limitLvl !=0 && building.getBuildingProxy().getCfg().getLevel() < limitLvl) {
					continue;
				}
				count++;
			}
			eventStack.setProgress(count);
			break;
		}
		}
	}
}
